/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package houduan;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.servlet.annotation.WebServlet;


@WebServlet(name = "updatePost", urlPatterns = {"/updatePost"})
public class UpdatePostServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UeditorServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UeditorServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
            
    // 设置请求和响应的编码格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        

        // 获取表单数据
        String postId = request.getParameter("id");
        String title = request.getParameter("title");
        String content = request.getParameter("editorValue");
        String category = request.getParameter("category");

        // 验证表单数据是否完整

            String DB_URL = "jdbc:mysql://localhost:3306/lczmysql?useUnicode=true&characterEncoding=UTF-8";
            String DB_USER = "root";
            String DB_PASSWORD = "123456";
            
            Connection connection = null;
            PreparedStatement statement = null;
            
            try {
                // 加载数据库驱动
                Class.forName("com.mysql.cj.jdbc.Driver");

                // 连接数据库
                connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
                
                // SQL 更新语句
                String sql = "UPDATE post SET title = ?, content = ?, category = ? WHERE id = ?";
                statement = connection.prepareStatement(sql);
                
                // 设置 SQL 参数
                statement.setString(1, title);
                statement.setString(2, content);
                statement.setString(3, category);
                statement.setInt(4, Integer.parseInt(postId));

                // 执行更新操作
                int result = statement.executeUpdate();
                
                // 输出更新结果
                PrintWriter out = response.getWriter();
                if (result > 0) {
                    out.println("<p>帖子更新成功！</p>");
                    out.println("<a href='./index.jsp?id=" + postId + "'>返回查看帖子</a>");
                } else {
                    out.println("<p>帖子更新失败！</p>");
                }
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
                response.getWriter().println("<p>数据库错误，请稍后再试。</p>");
            } finally {
                // 关闭资源
                try {
                    if (statement != null) {
                        statement.close();
                    }
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 可以调用 doPost 方法或者重定向到登录页面
        this.doPost(request, response);
    }
}
